<?php

namespace Admin\Controller;

use Think\Controller;

class LoginController extends Controller
{
    // 用户登录
    public function login()
    {
        if (D('User')->is_admin_login()) {
            $this->redirect('/main/index');
            exit();
        }
        if (IS_POST) {
            $username = I("post.username", '', 'trim');
            $password = I("post.password", '', 'trim');
            if (empty($username) || empty($password)) {
                $this->assign('err', '请填写用户名和密码');
                $this->display();
                exit();
            }
            $userinfo = D('User')->verify_username_password($username, $password);
            if ($userinfo && $userinfo['isadmin'] == '1' && $userinfo['auth']) {
                D('User')->user_login($userinfo);
                $this->redirect('/main/index');
            } else {
                $this->assign('err', '用户名或密码错误');
                $this->display();
            }
        } else {
            $this->display();
        }
    }

    // 退出登录
    public function logout()
    {
        D('User')->clear_session();
        $this->redirect('/login/login');
    }
}